package com.geezdata.cps.ttk_admin.api.controller;

import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.geezdata.cps.base.annotation.LoggerOut;
import com.geezdata.cps.base.enums.ErrorCode;
import com.geezdata.cps.base.response.BaseResponse;
import com.geezdata.cps.ttk.common.request.LoginRequest;
import com.geezdata.cps.ttk.common.response.LoginResult;
import com.geezdata.cps.ttk_admin.api.util.LoginManager;

@RestController
public class LoginController {
	@Autowired
    protected LoginManager loginManager;
    
    @LoggerOut
    @RequestMapping(value="/login", method=RequestMethod.GET) 
    public BaseResponse<Object> login() {
    	return BaseResponse.error(ErrorCode.INVALID_SESSION.getCode(), "SESSION失效，请重新登录"); 
    }
    
    @LoggerOut
    @RequestMapping(value="/unauthorized", method=RequestMethod.GET) 
    public BaseResponse<Object> unauthorized() {
    	return BaseResponse.error(ErrorCode.INVALID_AUTHORIZATION.getCode(), "当前用户没有权限"); 
    }
   
    @LoggerOut
    @ResponseBody
    @RequestMapping(value="/login", method=RequestMethod.POST)
    public BaseResponse<LoginResult> login(@Valid @RequestBody LoginRequest request){
        return loginManager.login(request.getUserName(), request.getPassword());
    }
    
    @RequestMapping(value="/logout", method=RequestMethod.POST)  
    public BaseResponse<String> logout(){
        //使用权限管理工具进行用户的退出，跳出登录，给出提示信息
        SecurityUtils.getSubject().logout(); 
        return BaseResponse.success("退出登录");
    }
}